package com.auerdi.ui.detail.cutprice.detail.child;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.auerdi.R;
import com.auerdi.ui.detail.utils.Utils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.egghttp.retrofit.beans.banner.BargainDatailResp;
import com.example.egghttp.retrofit.beans.banner.Img;
import com.example.egghttp.retrofit.beans.goods.CutProduct;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

public class ADCutDetailACompoDetail extends SupportFragment {

    @BindView(R.id.recy)
    RecyclerView recy;
    private List<Img> imgList = new ArrayList<>();

    public static ADCutDetailACompoDetail newInstance() {

        Bundle args = new Bundle();
        ADCutDetailACompoDetail fragment = new ADCutDetailACompoDetail();
        fragment.setArguments(args);
        return fragment;
    }

    Unbinder unbinder;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ad_cut_detail_a, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        recy.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) {

//            @Override
//            public boolean canScrollVertically() {
//                return false;
//            }

        });


        recy.setHasFixedSize(true);
//        recyLast.setLayoutManager(new GridLayoutManager(holder.getConvertView().getContext(), 2) {
//
//            @Override
//            public boolean canScrollVertically() {
//                return false;
//            }
//
//        });
//        recyLast.setHasFixedSize(true);
//        recy.setNestedScrollingEnabled(false);
//        recy.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        recy.setAdapter(new CommonAdapter<Img>(_mActivity, R.layout.item_decut_img_a, imgList) {
            @Override
            protected void convert(ViewHolder viewHolder, final Img item, int position) {

                ImageView imgView = viewHolder.getView(R.id.img);
//
//                Picasso.get().load(item.getImg_url()).error(R.mipmap.logo).placeholder(R.mipmap.logo).into(imgView);
//                Picasso.get().load(item.getPic_zhutu()).error(R.mipmap.vip).placeholder(R.mipmap.logo).into(img);

//                GlideApp.with(viewHolder.getConvertView().getContext())
//                        .load(item.getImg_url())
//                        .placeholder(R.mipmap.logo)
//                        .error(R.mipmap.pay_err)
//                        .fitCenter()
//                        .circleCrop()
//                        .into(imgView);


                Glide.with(getContext().getApplicationContext())
                        .asBitmap()
                        .load(item.getImg_url())
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap bitmap,
                                                        Transition<? super Bitmap> transition) {
                                int width = bitmap.getWidth();
                                int height = bitmap.getHeight();

                                Double radio = Double.valueOf(height / width);
                                Double HH = (double) Utils.getDisplayWidth((AppCompatActivity) getActivity());

                                imgView.setImageBitmap(bitmap);
                                ViewGroup.LayoutParams params = imgView.getLayoutParams();
                                params.height = ((Double) (HH * radio)).intValue();

                                imgView.setLayoutParams(params);

//                                radio = radio - Math.random() * radio / 3;

                            }
                        });
            }
        });

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(CutProduct event) {
    }


    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(BargainDatailResp event) {

        imgList.clear();
        imgList.addAll(event.getDetails_images_list());
        recy.getAdapter().notifyDataSetChanged();
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

}
